const app = getApp(),
  $extConfig = xhs.getExtConfigSync(),
  $config = require("../../config.js"),
  WxParse = require('../wxParse/wxParse.js'),
  $appId = $extConfig.appId || $config.appId,
  $host = $extConfig.host || $config.host,
  //业务域名
  $resHost = $extConfig.resHost || $config.resHost,
  appId = $extConfig.appId || $config.appId; //资源域名
Component({
  /**
   * 组件的属性列表a
   */
  properties: {
    isShowInfo: {
      type: Boolean,
      value: false
    },
    isShowCancel: {
      type: Boolean,
      value: false
    },
    userAgreement: {
      type: String,
      value: ''
    },
    focus: {
      type: Boolean,
      value: false
    }
  },
  attached() {
    app.$getData('/user/info/get-info').then(data => {
      if (data.errCode == 0) {
        this.setData({
          nickname: data.data.nickname == '游客' || data.data.nickname.substr(0, 5) == 'user_' ? '' : data.data.nickname,
          username: data.data.username,
          mobile: data.data.mobile || '',
          sex: data.data.sex == '0' ? '1' : data.data.sex,
          birthday: data.data.birthday,
          address: data.data.address,
          // briefIntroduction: data.data.briefIntroduction,
          'image.thumb': data.data.avatarCus.thumb || data.data.avatar,
          'image.materialId': data.data.avatarCus.thumb || data.data.avatar,
          shareImg: data.data.avatarCus.thumb || data.data.avatar,
          avatar: data.data.avatarCus.materialId || '',
          wechatName: xhs.getStorageSync('config').wechatInfo.wechatName,
          wechatLogo: xhs.getStorageSync('config').logo.url || this.data.defaultImg
        });
      }
    });
    let userAgreement = xhs.getStorageSync('config').wechatInfo ? xhs.getStorageSync('config').wechatInfo.userAgreement : '';
    if (userAgreement) {
      WxParse.wxParse('article', 'html', userAgreement, this, 5);
    }
  },
  data: {
    icon: `background: url(${$resHost}/img/renew/add.png) 170rpx center / 40rpx 40rpx no-repeat`,
    isShowXy: false,
    renewImg: `${$resHost}/img/renew/renew-success.jpg`,
    maleImg: `${$resHost}/img/renew/male.jpg`,
    femaleImg: `${$resHost}/img/renew/female.jpg`,
    defaultImg: `${$resHost}/img/renew/default.png`,
    wechatName: '',
    wechatLogo: '',
    image: {
      thumb: '',
      materialId: ''
    },
    avatar: '',
    nickname: '',
    username: '',
    mobile: '',
    //手机号码
    sex: '1',
    birthday: '',
    address: '',
    phone: '',
    vcode: '',
    //手机验证码
    hasValidMobile: '',
    //是否验证手机号码
    toggle: false,
    toggleTag: false,
    toggleGood: false,
    type: 0,
    userInfo: {},
    tagsList: [],
    tagsListData: [],
    briefIntroduction: '',
    ecGoodsList: [],
    ecGoodsListData: [],
    labelLength: 0,
    goodsLength: 0,
    ecGoodsIds: [],
    tagIds: [],
    shareImg: ''
  },
  /**
   * 组件的方法列表
   */
  methods: {
    setValue(e) {
      this.setData({
        [e.currentTarget.dataset.name]: e.detail.value
      });
    },
    //兼容type="nickname"取值
    sendUserInfo(e) {
      this.setData({
        nickname: e.detail.value.nickname
      });
      this.$sent();
    },
    cancelFn() {
      this.setData({
        isShowInfo: false
      });
    },
    onChooseAvatar(e) {
      if (e.detail.avatarUrl && e.detail.avatarUrl != '') {
        app.$proxyUploadFile('/upload/image', 'img', e.detail.avatarUrl, {}, `图片上传中`).then(data => {
          this.setData({
            avatar: data.materialId,
            shareImg: data.url
          });
        }, err => {
          console.log('失败' + JSON.stringify(data));
        });
      }
    },
    sexChange(e) {
      this.setData({
        sex: e.currentTarget.dataset.sex
      });
    },
    $sent() {
      /**
       * @api 更新用户基本信息
       * @param string avatar 头像
       * @param string username 用户名
       * @param int sex 性别1男2女
       * @param string birthday 出生日期
       * @param string address 地址 {$VAL="检测".date("Y-m-d H:i:s");}
       * @param int mobile 手机号码
       */
      let {
        image,
        username,
        vcode,
        address,
        mobile,
        birthday,
        nickname,
        sex,
        avatar,
        shareImg
      } = this.data;
      if (nickname == '' || shareImg.indexOf("default") != -1) return xhs.showToast({
        icon: 'none',
        title: '昵称和头像不能为空！'
      });
      var param = {};
      param = {
        avatar,
        username,
        nickname,
        sex,
        birthday,
        mobile,
        address,
        vcode
      };
      app.$postData('/user/info/update-info', param).then(data => {
        if (data.errCode == 0) {
          xhs.showToast({
            icon: 'none',
            title: '修改成功'
          });
          this.setData({
            isShowInfo: false
          });
          xhs.setStorageSync('authoriz', true);
          app.isEmployeeFn();
          this.triggerEvent('sub');
          this.triggerEvent('vipcard');
        } else {
          xhs.showToast({
            icon: 'none',
            title: data.errInfo
          });
        }
      });
    },
    closeX() {
      this.setData({
        isShowXy: false
      });
    },
    openXy() {
      this.setData({
        isShowXy: true
      });
    },
    handleTouchInput() {
      this.triggerEvent('getPrivacySetting');
      this.setData({
        isShowInfo: false
      });
    }
  }
});